package com.openspring.baseinfo.controller;

import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.openspring.baseinfo.model.Unit;
import com.openspring.framework.common.Paging;
import com.openspring.framework.controller.GenericController;
import com.openspring.framework.util.ResultUtils;
import com.openspring.baseinfo.service.UnitService;

@Controller
@RequestMapping("unit")
public class UnitController extends GenericController<Unit, Long> {
	
	private UnitService unitService;

	@Resource(name="unitService")
	public void setUnitService(UnitService unitService) {
		super.setGenericService(unitService);
		this.unitService = unitService;
	}
	
	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@ModelAttribute Unit entity) {
		unitService.create(entity);
		return ResultUtils.data(entity);
	}

	@RequestMapping(value="/modify", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@ModelAttribute Unit entity) {
		unitService.modify(entity);
		return ResultUtils.data(entity);
	}
	
	@RequestMapping(value="/remove", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> remove(@RequestParam("id") Long[] id) {
		if(id.length == 1)
			unitService.remove(id[0]);
		else
			unitService.remove(id);
		return ResultUtils.success();
	}
	
	@RequestMapping(value="/load/{id}", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> load(@PathVariable("id") Long id) {
		return ResultUtils.data(unitService.load(id));
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(
			@ModelAttribute Paging paging,
			@RequestParam(value="filter", required=false) String filter,
			@RequestParam(value="sort", required=false) String sort) {
		
		return this.doQuery(paging, filter, sort);
	}
}
